package com.samsung.controller.vote;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.samsung.common.util.BaseUtil;
import com.samsung.common.util.Constant;
import com.samsung.model.user.User;
import com.samsung.service.vote.TopicService;
import com.samsung.service.vote.VoteReportService;

@Controller
@RequestMapping("/vote")
public class VoteReportController  extends MultiActionController{

	@Resource
	private VoteReportService voteReportService;
	@Resource
	private TopicService topicService;
	
	/**
	 * 投票结果页面
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/voteResult")
	public ModelAndView statisticsPage(String topicId,HttpServletRequest request,HttpSession session) throws Exception{
		Map<String, Object> condition =  new HashMap<String, Object>();
		User user = (User)session.getAttribute(Constant.LOGIN_TOKEN);
		if (user == null) {
			String ip = BaseUtil.getRemoteHost(request);
			if (ip == null) {
				// >>>无法获取IP时处理逻辑>>>
				throw new Exception("无法获取您的IP");
			}else {
				condition.put("ip", ip);
			}
		}else{
			condition.put("uid", user.getUid());
		}
		condition.put("topicId",topicId);
		ModelAndView mav = new ModelAndView("vote/voteResult");
		Map<String, Object> reMap = new HashMap<String, Object>();
		Map<String, Object> topicMap = topicService.getTopicById(topicId);
		List<Map<String, Object>> optionMap = topicService.getOptionList(condition);
		reMap.put("topicMap", topicMap);
		reMap.put("optionMap", optionMap);
		mav.addObject("reMap", reMap);
		return mav;
	}
	
	@RequestMapping("ctVoteResult")
	public ModelAndView ctVoteDetail(String topicId,HttpServletRequest request,HttpSession session) throws Exception{
		Map<String, Object> condition =  new HashMap<String, Object>();
		String ip = BaseUtil.getRemoteHost(request);
		if (ip == null) {
			// >>>无法获取IP时处理逻辑>>>
			throw new Exception("无法获取您的IP");
		} else {
			condition.put("ip", ip);
		}
		condition.put("topicId",topicId);
		ModelAndView mav = new ModelAndView("vote/ctVoteResult");
		Map<String, Object> reMap = new HashMap<String, Object>();
		Map<String, Object> topicMap = topicService.getTopicById(topicId);
		List<Map<String, Object>> optionMap = topicService.getOptionList(condition);
		reMap.put("topicMap", topicMap);
		reMap.put("optionMap", optionMap);
		mav.addObject("reMap", reMap);
		return mav;
	}
	@RequestMapping("/getResultData")
	@ResponseBody
	public List<Map<String, Object>> statistics(String topicId){
		return topicService.getVoteResult(topicId);
	}
}
